using System;
using System.Web.Mvc;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Models.Binders;

namespace RMH.Web.Controllers
{
    public class DiagnosisController : ControllerBase
    {
        private IDiagnosisService diagnosisService;

        public DiagnosisController(IDiagnosisService diagnosisService)
        {
            this.diagnosisService = diagnosisService;
        }
        //
        // GET: /Diagnosis/

        public ViewResult DropDown()
        {
            return View(diagnosisService.GetAll());
        }
        [Authorize(Roles="Administrator")]
        public ActionResult Index()
        {
            return View(diagnosisService.GetAll());
        }

        //
        // GET: /Diagnosis/Details/5

        [Authorize(Roles = "Administrator")]
        public ViewResult Details(int? id)
        {
            return View(diagnosisService.Get(id.Value));
        }

        //
        // GET: /Diagnosis/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int? id)
        {
            Diagnosis diagnosis;
            if (!id.HasValue)
            {
                diagnosis = new Diagnosis();
            }
            else
            {
                diagnosis = diagnosisService.Get(id.Value);
            }
            return View(diagnosis);
        }

        //
        // POST: /Diagnosis/Edit/5

        [Authorize(Roles = "Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([ModelBinder(typeof(DiagnosisBinder))] Diagnosis diagnosis)
        {
            if (String.IsNullOrEmpty(diagnosis.Name.Trim()))
                ModelState.AddModelError("Name", "Name is required.");
            if (!ModelState.IsValid)
            {
                diagnosisService.Evict(diagnosis);
                return View(diagnosis);
            }
            diagnosisService.Save(diagnosis);
            return RedirectToAction("Index");
        }
    }
}
